//-----------------------------------------------------------------------
// <copyright file="HelpCatDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class HelpCatDAOBase
    {
        #region Common methods
        public virtual HelpCat CreateHelpCatFromReader(IDataReader reader)
        {
            HelpCat item = new HelpCat();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("CatId"))) item.CatId = (long)reader["CatId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Name"))) item.Name = (string)reader["Name"];
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) item.Description = (string)reader["Description"];
                if (!reader.IsDBNull(reader.GetOrdinal("SortOrder"))) item.SortOrder = (long)reader["SortOrder"];
                if (!reader.IsDBNull(reader.GetOrdinal("Enable"))) item.Enable = (bool)reader["Enable"];
                if (!reader.IsDBNull(reader.GetOrdinal("Parrentid"))) item.Parrentid = (long)reader["Parrentid"];
                if (!reader.IsDBNull(reader.GetOrdinal("CatLevel"))) item.CatLevel = (int)reader["CatLevel"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateHelpCatFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateHelpCat methods
            
        public virtual void CreateHelpCat(HelpCat helpCat)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spHelpCatInsert");
                
                database.AddInParameter(dbCommand, "@Name", DbType.String, helpCat.Name);
                database.AddInParameter(dbCommand, "@Description", DbType.String, helpCat.Description);
                database.AddInParameter(dbCommand, "@SortOrder", DbType.Int64, helpCat.SortOrder);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, helpCat.Enable);
                database.AddInParameter(dbCommand, "@Parrentid", DbType.Int64, helpCat.Parrentid);
                database.AddInParameter(dbCommand, "@CatLevel", DbType.Int32, helpCat.CatLevel);
                database.AddOutParameter(dbCommand, "@CatId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                helpCat.CatId = (long)database.GetParameterValue(dbCommand, "@CatId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateHelpCatException, ex);
            }
        }

        #endregion

        #region UpdateHelpCat methods
        
        public virtual void UpdateHelpCat(HelpCat helpCat)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spHelpCatUpdate");            
                
                database.AddInParameter(dbCommand, "@CatId", DbType.Int64, helpCat.CatId);
                database.AddInParameter(dbCommand, "@Name", DbType.String, helpCat.Name);
                database.AddInParameter(dbCommand, "@Description", DbType.String, helpCat.Description);
                database.AddInParameter(dbCommand, "@SortOrder", DbType.Int64, helpCat.SortOrder);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, helpCat.Enable);
                database.AddInParameter(dbCommand, "@Parrentid", DbType.Int64, helpCat.Parrentid);
                database.AddInParameter(dbCommand, "@CatLevel", DbType.Int32, helpCat.CatLevel);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateHelpCatException, ex);
            }
        }
        
        #endregion

        #region DeleteHelpCat methods
        public virtual void DeleteHelpCat(long catId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spHelpCatDelete");
                
                database.AddInParameter(dbCommand, "@CatId", DbType.Int64, catId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteHelpCatException, ex);
            }
        }

        #endregion

        #region GetHelpCat methods
        
        public virtual HelpCat GetHelpCat(long catId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spHelpCatGet");
                
                database.AddInParameter(dbCommand, "@CatId", DbType.Int64, catId);
                
                HelpCat helpCat =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        helpCat = CreateHelpCatFromReader(reader);
                        reader.Close();
                    }
                }
                return helpCat;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetHelpCatException, ex);
            }
        }
        
        #endregion

        #region GetHelpCatList methods
        public virtual HelpCatCollection GetHelpCatList(HelpCatColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spHelpCatGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                HelpCatCollection helpCatCollection = new HelpCatCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        HelpCat helpCat = CreateHelpCatFromReader(reader);
                        helpCatCollection.Add(helpCat);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return helpCatCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetHelpCatListException, ex);
            }
        }
        
        public virtual HelpCatCollection GetHelpCatList(HelpCatColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetHelpCatList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetHelpCatList methods
        public virtual HelpCatCollection GetHelpCatListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spHelpCatGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                HelpCatCollection helpCatCollection = new HelpCatCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        HelpCat helpCat = CreateHelpCatFromReader(reader);
                        helpCatCollection.Add(helpCat);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return helpCatCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetHelpCatListException, ex);
            }
        }
        
        public virtual HelpCatCollection GetHelpCatListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetHelpCatListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}